let express = require('express');
let app = express();
app.use(express.static(__dirname));
let whitList = ['http://localhost:3000'];
app.use(function (req, res, next) {
    var origin = req.headers.origin;
    if (whitList.includes(origin)) {
        // 允许哪个源访问我
        res.setHeader('Access-Control-Allow-Origin', origin);
        // 允许携带哪个头访问我
        res.setHeader('Access-Control-Allow-Headers', 'name');
        // 允许哪个方法访问我
        res.setHeader('Access-Control-Allow-Methods', 'PUT');
        // 允许携带cookie
        res.setHeader('Access-Control-Allow-Credentials', true);
        // 允许前端获取哪个头
        res.setHeader('Access-Control-Expose-Headers', 'name');
        if (req.methods == 'OPTIONS') {
            res.end();
        }
    }
    next();
})
app.get('/getData', function (req, res) {
    console.log(req.headers)
    res.setHeader('name', 'zspx')
    res.end('我不爱你')
})
app.put('/getData', function (req, res) {
    console.log(req.headers)

    res.end('我不爱你')
})
app.listen(4000, () => {
    console.log('ser2启动成功')
})    